﻿using ReflectionStudio.Core.Events;
using System;
using System.Data;
using System.Windows;
using System.Windows.Data;
using System.ComponentModel;
using WPFExcelReport.Helpers;
using System.Data.SqlClient;
using System.Data.Sql;
using System.Windows.Input;
using System.Collections;
using System.Windows.Controls;
namespace WPFExcelReport
{
    public partial class TGB14RED
    {
        #region ==FROMDATE===========
        public static readonly DependencyProperty FROMDATEProperty = DependencyProperty.Register("FROMDATE",
            typeof(string), typeof(TGB14RED),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnFROMDATEChanged));
        public string FROMDATE
        {
            get
            {
                return (string)GetValue(FROMDATEProperty);
            }
            set
            {
                SetValue(FROMDATEProperty, value);
            }
        }
        private static void OnFROMDATEChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TGB14RED;
            if (src != null)
            {
                src.FROMDATE = (string)e.NewValue;
            }
        }
        #endregion

        #region ==ENDDATE===========
        public static readonly DependencyProperty ENDDATEProperty = DependencyProperty.Register("ENDDATE",
            typeof(string), typeof(TGB14RED),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnENDDATEChanged));
        public string ENDDATE
        {
            get
            {
                return (string)GetValue(ENDDATEProperty);
            }
            set
            {
                SetValue(ENDDATEProperty, value);
            }
        }
        private static void OnENDDATEChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TGB14RED;
            if (src != null)
            {
                src.ENDDATE = (string)e.NewValue;
            }
        }
        #endregion
        public TGB14RED()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VGLLG();
            this.DataContext = MV;
            InitializeComponent();
        }

        public override void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            if (IsLoaded)
                return;
            base.Window_Loaded(sender, e);
        }

        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);//

            if (dwMaster == null)
            {
                KEY = "";

            }
            else
            {
                //KEY = TMV.GLLBEHI + TMV.GLLO + TMV.GLLB + "LIKE '" + TMV.GLLP.Trim() + "%'";
            }
        }

        public override void QueryCommand(object sender, RoutedEventArgs e)
        {
            base.QueryCommand(sender, e);
            dwData = TBMain.NewRow();
            if (FROMDATE==""||FROMDATE==null)
            {
                FROMDATE = "20000101";
            }
            if (ENDDATE==null||ENDDATE=="")
            {
                ENDDATE = "20991231";
            }
            dwData["GLLO"] = LO.Trim();
            dwData["GLLB"] = LB.Trim();
            dwData["GLLP"] = LP.Trim();
            //dwData["GLMANDANT"] = app.Mandant;
            if (ART.Trim()==""||ART.Trim()==null)
            {
                dwData["GLBEM"] = " LIKE '%'";

            }
            else
            {
                dwData["GLBEM"] = " LIKE '" + ART.Trim() + "%'";

            }
            
            dwData["GLENTDAT"] = FROMDATE.Trim();
            dwData["GLUBDAT"] = ENDDATE.Trim();
            
            //dwData["GLLB"] = LB;
            //dwData["GLLP"] = "LIKE '"+LP.Trim()+"%'";
            SResult = TMV.QGLLG1301(dwData);
            if (SResult.Tables.Count>0)
            {
                string typ = "";
                foreach (DataRow dw in SResult.Tables[0].Rows)
                {
                    typ = dw["GABBS"].ToString();                  
                   
                    if (typ == "61")
                    {
                        dw["ABC"] = "留位"; 
                    }
                    if (typ == "62")
                    {
                        dw["ABC"] = "取消留位"; 
                    }
                    if (typ == "38")
                    {
                        dw["ABC"] = "生产消耗"; 
                    }
                    if (typ == "20")
                    {
                        dw["ABC"] = "库存移除"; 
                    }
                    if (typ == "21")
                    {
                        dw["ABC"] = "库存转入"; 
                    }
                    if (typ == "22")
                    {
                        dw["ABC"] = "生产发料接收"; 
                    }
                    if (typ == "11")
                    {
                        dw["ABC"] = "采购入库"; 
                        
                    }
                    if (typ == "42")
                    {
                        dw["ABC"] = "库存减少"; 
                    }
                    if (typ == "43")
                    {
                        dw["ABC"] = "库存增加"; 
                    }
                   
                    if (typ == "34")
                    {
                        dw["ABC"] = "生产消耗"; 
                    }
                    if (typ == "33")
                    {
                        dw["ABC"] = "成本中心消耗"; 
                    }
                  
                    if (typ == "35")
                    {
                        dw["ABC"] = "生产退回"; 
                    }
                    if (typ == "36")
                    {
                        dw["ABC"] = "消耗"; 
                    }
                    if (typ == "37")
                    {
                        dw["ABC"] = "消耗轴"; 
                    }
                   
                    if (typ == "01")
                    {
                        dw["ABC"] = "贸易入"; 
                    }
                   
                }
                
            }
            MergeData(SResult, true);

        }

        public void QUERY_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                this.Focus();
                QueryCommand(null, null);
            }

        }
        private void FROM_DATE_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                //FROMDATE = "";
                FROMDATE = Helpers.Helper.GetDate(sender);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }

        }
        private void END_DATE_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                //ENDDATE = "";
                ENDDATE = Helpers.Helper.GetDate(sender);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }

        }

    }
}
